/**
 * @file 工单操作-工单照片
 * @author songweidong
 * @date 2019-07-10 16:20:25
 */
layui.define(['layer', 'upload', 'form', 'list_modify_common', '$http','previewer'], function (exports) {
    let $ = layui.$;
    let form = layui.form;
    let opCommonUtils = layui.list_modify_common;
    let $http = layui.$http;
    let layer = layui.layer;
    let upload = layui.upload;
    var previewer = layui.previewer;
    let timestamp = new Date().getTime();

    function submit(data) {

        let url = "/work/modify/save/orderPhoto.action";
        // 处理附件数据

        /* 遍历所有的照片，如果某个照片类型是【必传】且【有照片未上传】，就不让提交。 */
        let attachments = processAttachmentObject(data);
         if(attachments){
             data.field.attachmentList = attachments;
             $http.lockSave(url, data.field, data.elem).then(function () {
                 layer.msg('操作成功', {icon: 1, time: 1000}, function () {
                     opCommonUtils.goOperateHome();
                 });
             }, function (msg) {
                 layer.msg(msg);
             });
         }
    }

    /**
     * 验证表单
     */
    function initFormVerify () {
        form.verify({
            // 必填校验
            otherReq: function(value,item){
                var $ = layui.$;
                var verifyName=$(item).attr('name')
                    , verifyType=$(item).attr('type')
                    ,formElem=$(item).parents('.layui-form')//获取当前所在的form元素，如果存在的话
                    ,verifyElem=formElem.find('input[name='+verifyName+']')//获取需要校验的元素
                    ,isTrue= verifyElem.is(':checked')//是否命中校验
                    ,focusElem = verifyElem.next().find('i.layui-icon');//焦点元素
                if(!isTrue || !value){
                    //定位焦点
                    focusElem.css(verifyType=='radio'?{"color":"#FF5722"}:{"border-color":"#FF5722"});
                    //对非输入框设置焦点
                    focusElem.first().attr("tabIndex","1").css("outline","0").blur(function() {
                        focusElem.css(verifyType=='radio'?{"color":""}:{"border-color":""});
                    }).focus();
                    return '必填项不能为空';
                }
            }
        })
    }

    /**
     * 处理附件数据
     * @param data
     */
    function processAttachmentObject(data) {
        let attachmentList = [];

        let attachmentElementList = $(".attachmentList");
        for (let i = 0; i < attachmentElementList.length; i++) {
            let groupItem = attachmentElementList[i];
            // 找到当前【照片类型】下的所有attachment
            let groupDetail = $(groupItem).find(".group-detail");

            for (let j = 0; j < groupDetail.length; j++) {
                let detailItem = groupDetail[j];

                // 组装数据
                let attachment = {};
                let attachmentId = $(detailItem).find("input[name='attachmentId']").val();
                let reviewStatus = $(detailItem).find("input[name='reviewStatus-" + attachmentId + "']:checked").val();

                attachment.attachmentId = attachmentId;
                attachment.reviewStatus = reviewStatus;
                attachmentList.push(attachment);
            }
            console.log(attachmentList)
        }
        return attachmentList;
    }

    /**
     * 初始化页面
     */
    function initPage() {

        // 初始化表单
        initFormVerify();

    }

    /**
     * 初始化页面事件
     */
    function initEvent() {

        // 初始化图片预览器
        previewer.initTextList('.eg-images');
        previewer.initTextList('.attachment-images');
        previewer.initTextList('.smile-action-image');

        // AI审核失败原因
        $(document).on('click', ".audit-reason", function () {
            let auditDesc = $(this).attr("audit-desc");
            layer.msg("AI审核不合格原因：" + auditDesc);
        });

        // 保存
        form.on('submit()', function (data) {
            submit(data);
            return false
        });
    }

    // 程序入口
    function init() {

        console.log("入口")
        // 初始化页面
        initPage();

        // 初始化事件
        initEvent();

    }

    // 初始化
    init();
    exports('oms_work_modify_orderphoto');
});
